﻿using System;
using System.Buffers.Binary;

namespace WholeSlideImageLibrary.Formats.Dmetrix
{
    internal struct DmetrixFileLayerIndex
    {
        /// <summary>
        /// Current layer.
        /// </summary>
        public short Layer { readonly get; private set; }
        /// <summary>
        /// Max line.
        /// </summary>
        public int MaxColumn { readonly get; private set; }
        /// <summary>
        /// Max row.
        /// </summary>
        public int MaxRow { readonly get; private set; }
        /// <summary>
        /// Position of the begining of this layer in the file stream.
        /// </summary>
        public int LayerStartPosition { get; private set; }

        internal const int MarshalStructureSize = 14;

        public static bool TryParse(ReadOnlySpan<byte> buffer, out DmetrixFileLayerIndex data)
        {
            data = default;
            if (buffer.Length < MarshalStructureSize)
            {
                return false;
            }
            data.Layer = BinaryPrimitives.ReadInt16LittleEndian(buffer);
            data.MaxColumn = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(2));
            data.MaxRow = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(6));
            data.LayerStartPosition = BinaryPrimitives.ReadInt32LittleEndian(buffer.Slice(10));
            return true;
        }
    }
}
